package com.example.guardian.client.service.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.example.guardian.client.manager.TransactionMessageManager;
import com.example.guardian.client.message.MessageStatus;
import com.example.guardian.client.message.TransactionMessageService;
import com.example.guardian.client.message.model.MessageModel;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 事务消息service实现
 *
 * @author gang.wang 2021/10/13
 */
@Service
public class TransactionMessageServiceImpl implements TransactionMessageService {

    @Autowired
    private TransactionMessageManager transactionMessageManager;

    @Override
    public boolean initPreMessage(MessageModel messageModel) {
        return transactionMessageManager.add(messageModel);
    }

    @Override
    public boolean confirmSendMessage(String messageId) {
        MessageModel messageModel = transactionMessageManager.getByMessageId(messageId);
        messageModel.setStatus(MessageStatus.CONFIRM.name());
        transactionMessageManager.update(messageModel);
        return true;
    }

    @Override
    public boolean confirmConsumerMessage(String messageId) {
        MessageModel messageModel = transactionMessageManager.getByMessageId(messageId);
        messageModel.setStatus(MessageStatus.FINISH.name());
        transactionMessageManager.update(messageModel);
        return true;
    }
}
